Working with EntryIDs and StoreIDs

If you’re creating a more complex solution using Microsoft Outlook that involves linking or cross-referencing items in folders, one option is to use the MAPI-based identifiers (IDs) of each item. If you know the IDs of an item and the folder it's stored in, you can directly reference the item using the GetItemFromID method.

Each Outlook item has a field called EntryID, which is a unique ID field generated by the messaging storage system for use with the MAPI folders that store the item. It’s important to note that whenever an item is created in a folder, it’s assigned a new EntryID. This means that the EntryID field changes if an item is moved to a different folder or if an item is exported and then imported (even to the same folder).

Each folder has an ID field called StoreID, the value of which is the same for all the folders in a particular message store. Each folder also has a unique EntryID field.

When using the GetItemFromID method to retrieve an item based on its IDs, you should specify both the EntryID of the item and the StoreID of the folder. If you do not specify the StoreID, GetItemFromID searches the default message store.

The following Microsoft Visual Basic/Visual Basic for Applications (VBA) example illustrates the use of the GetItemFromID method. The code retrieves the StoreID from the default Contacts folder, fills an array (MyEntryID) with the EntryIDs of all of the contacts in the folder, and finally retrieves a specific contact item.

Sub OutlookEntryID()
   Dim ol As Outlook.Application
   ' If there are more than 500 contacts, change the following line:
   Dim MyEntryID(500) As String
   Dim StoreID As String
   Dim EntryID As String
   Set ol = New Outlook.Application
   Set olns = ol.GetNamespace("MAPI")
   Set objFolder = olns.GetDefaultFolder(olFolderContacts)
   ' Get the StoreID, which is a property of the folder.
   StoreID = objFolder.StoreID
   ' Set objAllContacts equal to the collection of all contacts.
   Set AllContacts = objFolder.Items
   I = 0
   ' Loop to get all of the EntryIDs for the contacts.
   For Each Item In AllContacts
      I = I + 1
      MyEntryID(I) = Item.EntryID
   Next
   ' Randomly choose the 2nd contact to retrieve.
   Set Item = olns.GetItemFromID(MyEntryID(2), StoreID)
   Item.Display
End Sub